package hotLeetcode;

import java.util.Deque;
import java.util.LinkedList;

public class H994Leetcode {
    public int orangesRotting(int[][] grid) {
        //首先用队列来保存橘子
        int M = grid.length;
        int N = grid[0].length;
        int count = 0; //新鲜橘子的数量
        Deque<int[]> queue = new LinkedList<>();
        for (int r = 0; r < M; r++) {
            for (int c = 0; c < N; c++) {
                if (grid[r][c] == 1) {
                    count++;
                } else if (grid[r][c] == 2) {
                    queue.add(new int[]{r, c});
                }

            }
        }


        int round = 0;
        while (count > 0 && !queue.isEmpty()) {
            round++;
            int n = queue.size();
            for (int i = 0; i < n; i++) {
                int[] orange = queue.poll();
                int r = orange[0];
                int c = orange[1];
                if (r - 1 >= 0 && grid[r - 1][c] == 1) {
                    grid[r - 1][c] = 2;
                    count--;
                    queue.add(new int[]{r - 1, c});
                }
                if (r+1 < M && grid[r+1][c] == 1) {
                    grid[r+1][c] = 2;
                    count--;
                    queue.add(new int[]{r+1, c});
                }

                if (c-1 >= 0 && grid[r][c-1] == 1) {
                    grid[r][c-1] = 2;
                    count--;
                    queue.add(new int[]{r, c-1});
                }
                if (c+1 < N && grid[r][c+1] == 1) {
                    grid[r][c+1] = 2;
                    count--;
                    queue.add(new int[]{r, c+1});
                }




            }


        }



        if (count > 0) {
            return -1;
        } else {
            return round;
        }



    }


    public static void main(String[] args) {
        int[][] grid = {{2, 1, 1}, {1, 1, 2}, {2, 0, 1}};
        H994Leetcode h994Leetcode = new H994Leetcode();
        System.out.println(h994Leetcode.orangesRotting(grid));
    }



}
